﻿/*******************************************************************************
#                    ADMSiteMessageBrokerBroker.cs
#  Comment:
#
#  Current Version: V1.0
#  Author: 
#
#  History List:
#  V1.0    Created by Onions@2009-9-17 10:22:15
#
#******************************************************************************/
#region Usings
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using SYS.Basic;

using ExpertMIS.Brokers;
using ExpertMIS.Entities;
using System.Collections;
#endregion

namespace ExpertMIS.Brokers
{
    public class ADMSiteMessageBroker: ExpertMISBaseBroker
    {
        #region Private Methods

        private object UpdateADMSiteMessageEntity(IEntity entity)
        {
            return SqlMap.Update("UpdateADMSiteMessageEntity", entity);
        }

        private object InsertADMSiteMessageEntity(IEntity entity)
        {
            return SqlMap.Insert("InsertADMSiteMessageEntity", entity);
        }

        private object DeleteADMSiteMessageEntity(IEntity entity)
        {
            return SqlMap.Delete("DeleteADMSiteMessageEntity", entity);
        }

        #endregion

        #region Override Methods

        protected override IEntity ProcessGetEntity(System.Collections.Hashtable keys)
        {
			if (!keys.Contains("OID")) throw new Exception("invalid request parameters.");
            return SqlMap.QueryForObject<IEntity>("SelectADMSiteMessageEntityByOID", keys["OID"] as string);
        }

        protected override bool ProcessSaveEntity(IEntity entity, Hashtable request)
        {
            if (entity.EntityState == EntityState.New)
            {
                InsertADMSiteMessageEntity(entity);
                entity.EntityState = EntityState.Old;
            }
            else
            {
                UpdateADMSiteMessageEntity(entity);
            }

            return true;
        }

        protected override IEntity ProcessNewEntity()
        {
            ADMSiteMessageEntity entity = new ADMSiteMessageEntity();
			
			// initial entity object

            return entity;
        }

        protected override bool ProcessRemoveEntity(IEntity entity, Hashtable request)
        {
            try
            {
                DeleteADMSiteMessageEntity(entity);
                return true;
            }
            catch
            {
                return false;
            }
        }

        #endregion

        #region Public Method

        /// <summary>
        /// 获取所有站内信息
        /// </summary>
        /// <returns></returns>
        public IList<ADMSiteMessageEntity> GetAllSiteMessages()
        {
            return SqlMap.QueryForList<ADMSiteMessageEntity>("SelectAllSiteMessages", null);
        }

        /// <summary>
        /// 根据用户ID，获取所有站内消息
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public IList<ADMSiteMessageEntity> GetAllSiteMessageByUser(string userID)
        {
            if (string.IsNullOrEmpty(userID))
            {
                return null;
            }

            return SqlMap.QueryForList<ADMSiteMessageEntity>("SelectAllSiteMessagesByUserID", userID);
        }

        /// <summary>
        /// 根据用户ID及是否阅读标志，获取用户站内消息
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="isRead"></param>
        /// <returns></returns>
        public IList<ADMSiteMessageEntity> GetSiteMessageByUser(string userID, bool isRead)
        {
            Hashtable hsQuery = new Hashtable();
            hsQuery.Add("user", userID);
            hsQuery.Add("isread", isRead ? 1 : 0);

            return SqlMap.QueryForList<ADMSiteMessageEntity>("SelectSiteMessageByUserID", hsQuery);
        }

        #endregion
    }
}
